# ------------------
# Public variables
CSC ?= gmcs
MONO ?= mono
GENDARME ?= /usr/local/bin/gendarme
MULTIMETHOD ?= multimethod
INSTALL_DIR ?= /usr/local/bin
MAN_DIR ?= /usr/local/share/man

ifdef RELEASE
	# Note that -debug+ just generates an mdb file.
	CSC_FLAGS ?= -checked+ -debug+ -optimize+ -warn:4 -d:TRACE -d:CONTRACTS_PRECONDITIONS
else
	CSC_FLAGS ?= -checked+ -debug+ -warn:4 -warnaserror+ -d:DEBUG -d:TRACE -d:CONTRACTS_FULL
endif

CSC_FLAGS += -nowarn:1591	# Missing XML comment for publicly visible type or member

# ------------------
# Internal variables
dummy1 := $(shell mkdir bin 2> /dev/null)
dummy2 := $(shell if [[ "$(CSC_FLAGS)" != `cat bin/csc_flags 2> /dev/null` ]]; then echo "$(CSC_FLAGS)" > bin/csc_flags; fi)

base_version := 0.2.xxx.0										# major.minor.build.revision
version := $(shell ./get_version.sh $(base_version) build_num)	# this will increment the build number stored in build_num
version := $(strip $(version))
export version

# ------------------
# Primary targets
all: app

app: bin/mmmdoc.exe

generate: app
	"$(MONO)" --debug bin/mmmdoc.exe --out=docs --verbose --extra-dict=mmmdoc.dict --visibility=internal bin/mmmdoc.exe,bin/docs.xml

multi-methods:
	"$(MULTIMETHOD)" --result=void --name=SetSuffix --arg=TypeSpec:instance --arg=TypeSpecification:spec --dst=source/database/types --using=Mono.Cecil --namespace=MmmDoc.Database --comment='/// <summary>Multimethod used by <see cref = "TypeSpec"/>.</summary>'
	"$(MULTIMETHOD)" --result=void --name=WriteCodeElement --arg=Writer:writer --arg=CodeElement:element --arg=string:description --arg=TypeDef:declaringType --dst=source/html --using=MmmDoc.Database --namespace=MmmDoc.Html --comment='/// <summary>Multimethod used to write out a <see cref = "CodeElement"/>.</summary>'
	"$(MULTIMETHOD)" --result=void --name=WriteMarkup --arg=Writer:writer --arg=Markup:markup --arg=bool:sanitize --arg=TypeDef:declaringType --dst=source/html --using=MmmDoc.Database --namespace=MmmDoc.Html --comment='/// <summary>Multimethod used to write out <see cref = "Markup"/>.</summary>'
	"$(MULTIMETHOD)" --result=void --name=WriteSignature --arg=Writer:writer --arg=CodeElement:member --arg=bool:sanitize --dst=source/html --using=MmmDoc.Database --namespace=MmmDoc.Html --comment='/// <summary>Multimethod used to write out HTML for member signatures.</summary>'
	"$(MULTIMETHOD)" --result=void --name=WriteType --arg=Writer:writer --arg=TypeName:type --arg=bool:sanitize --dst=source/html --using=MmmDoc.Database --namespace=MmmDoc.Html --comment='/// <summary>Multimethod used to write out HTML for types.</summary>'

update-libraries:
	cp `pkg-config --variable=Sources mono-options` source

# Note that this depends on some environment variable that isn't set when it is executed
# from within Foreshadow.
docs: mmmdoc.1 README.html

# ------------------
# File targets 
sources := source/*.cs source/database/*.cs source/database/members/*.cs source/database/markup/*.cs source/database/types/*.cs source/html/*.cs
resources := -resource:source/html/style-sheets/namespaces-file.css -resource:source/html/style-sheets/namespaces-toc.css -resource:source/html/style-sheets/type-file.css -resource:source/html/style-sheets/type-toc.css
references := -r:bin/Mono.Cecil.dll,System.Web

bin/mmmdoc.exe: bin/csc_flags $(sources) source/html/style-sheets/*.css
	@./gen_version.sh $(version) source/AssemblyVersion.cs
	$(CSC) -out:bin/mmmdoc.exe $(CSC_FLAGS) -doc:bin/docs.xml $(resources) $(references) -target:exe $(sources)

mmmdoc.1: mmmdoc.1.ascii
	a2x -f manpage --attribute=manversion=$(version) --attribute=manpage mmmdoc.1.ascii

README.html: mmmdoc.1.ascii
	a2x -f xhtml --attribute=manversion=$(version) mmmdoc.1.ascii
	mv mmmdoc.1.html README.html

# ------------------
# Misc targets
gendarme_flags := --severity all --confidence all --ignore gendarme.ignore --quiet
gendarme: bin/mmmdoc.exe
	@-"$(GENDARME)" $(gendarme_flags) bin/mmmdoc.exe

clean:
	-rm -rf bin/mmmdoc.exe
	-rm -rf bin/mmmdoc.exe.mdb
	-rm  -f bin/csc_flags
	-rm  -f bin/docs.xml

distclean:
	-rm -rf bin/mmmdoc.exe
	-rm -rf bin/mmmdoc.exe.mdb
	-rm  -f bin/csc_flags
	-rm  -f bin/docs.xml
	-rm  -f build_num
	-rm -rf docs

# Note that we don't want any dependencies here so that people can install without having to build.
install:
	@install -d "$(INSTALL_DIR)/mmmdoc_files"
	install "bin/mmmdoc.exe" "$(INSTALL_DIR)/mmmdoc_files"
	install "bin/Mono.Cecil.dll" "$(INSTALL_DIR)/mmmdoc_files"
	install "mmmdoc.1" "$(MAN_DIR)/man1"
ifndef RELEASE
	install "bin/mmmdoc.exe.mdb" "$(INSTALL_DIR)/mmmdoc_files"
	install "bin/Mono.Cecil.dll.mdb" "$(INSTALL_DIR)/mmmdoc_files"
endif
	@-rm -f "$(INSTALL_DIR)/mmmdoc"
	echo 'mono "$(INSTALL_DIR)/mmmdoc_files/mmmdoc.exe"  "\x24@"' > "$(INSTALL_DIR)/mmmdoc"
	chmod +x "$(INSTALL_DIR)/mmmdoc"

uninstall:
	-rm "$(INSTALL_DIR)/mmmdoc"
	-rm -rf "$(INSTALL_DIR)/mmmdoc_files"
	-rm "$(MAN_DIR)/man1/mmmdoc.1"

help:
	@echo "mmmdoc version $(version)"
	@echo " "
	@echo "The primary targets are:"
	@echo "app              - builds the application"
	@echo "docs             - generates the man page and README.html"
	@echo "generate         - generates html docs for mmmdoc"
	@echo "multi-methods    - generates the multimethod classes"
	@echo "update-libraries - uses pkg-config to copy libraries mmmdoc depends upon"
	@echo "clean            - removes generated files from the bin directory"
	@echo "install          - copies the exe to $(INSTALL_DIR)"
	@echo "uninstall        - removes the exe from $(INSTALL_DIR)"

dist: app
	tar --create --compress --exclude \*/.svn --exclude \*/.svn/\* --file=mmmdoc-$(version).tar.gz \
		BUILDING MIT.X11 Makefile README.html gen_version.sh gendarme.ignore get_version.sh mmmdoc.dict mmmdoc.1 mmmdoc.1.ascii source  bin/mmmdoc.exe bin/mmmdoc.exe.mdb bin/Mono.Cecil.dll bin/Mono.Cecil.dll.mdb
